import 'package:cached_network_image/cached_network_image.dart';
import 'package:chatgreat/helper/constant.dart';
import 'package:chatgreat/helper/image.dart';
import 'package:chatgreat/helper/my_widget.dart';
import 'package:chatgreat/page/component/enhanced_button.dart';
import 'package:chatgreat/page/component/image.dart';
import 'package:chatgreat/page/component/weak_text_button.dart';
import 'package:chatgreat/page/component/dialog.dart';
import 'package:chatgreat/page/component/theme/custom_theme.dart';
import 'package:chatgreat/repo/api/room_gallery.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';

import '../../repo/api_server.dart';

class RoomCard extends StatelessWidget {
  final bool selected;
  final RoomGallery item;
  final Function(RoomGallery) onItemSelected;
  final Function()? selectedCheck;
  final double fontsize;
  final bool stopAllEvents;
  const RoomCard({
    super.key,
    this.selected = false,
    required this.item,
    required this.onItemSelected,
    this.fontsize = 13,
    this.selectedCheck,
    this.stopAllEvents = false,
  });

  Future openRoomCard(BuildContext context, RoomGallery item) {
    return openModalBottomSheet(
      context,
      (_) {
        return Container(
          padding: const EdgeInsets.only(top: 20),
          child: GalleryRoomCard(
            item: item,
            selected: selected,
            // onConfirm: () {
            //   onItemSelected(item);
            // },
          ),
        );
      },
      heightFactor: 0.7,
      disableCompleteEvent: true,
      disableEvent: stopAllEvents,
    );
  }

  @override
  Widget build(BuildContext context) {
    var customColors = Theme.of(context).extension<CustomColors>()!;
    return InkWell(
      onLongPress: () async {
        // await openRoomCard(context, item);
        // selectedCheck?.call();
      },
      onTap: () {
        onItemSelected(item);
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(11),
          image: DecorationImage(
            image: CachedNetworkImageProvider(item.cover),
            fit: BoxFit.cover, // 根据需要选择合适的BoxFit
          ),
        ),
        padding: const EdgeInsets.only(left: 11, right: 9, bottom: 9),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Spacer(),
            Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      MyTextSingleScale(item.name, 14, Colors.white, FontWeight.w500),
                      const SizedBox(height: 3),
                      MyTextSingleScale(item.remark, 12, Colors.white.withOpacity(0.8), FontWeight.normal),
                    ],
                  ),
                ),
                InkWell(
                  onTap: () async {
                    print('--------> 点击语音');
                    EasyLoading.show(status: '加载中...');
                    try {
                      // 创建房间
                      int id = await APIServer().createRoom(roleId: item.id, name: item.name);
                      EasyLoading.dismiss();
                      context.push('/home_speeh_chat', extra: {
                        'name': item.name,
                        'avater': item.avatarUrl,
                        'roomId': id,
                      });
                    } catch (e) {
                      EasyLoading.showError(e.toString());
                    }
                  },
                  child: imgWidget(30.w, 30.w, 'microphone_icon'),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class GalleryRoomCard extends StatelessWidget {
  final RoomGallery item;
  final Function()? onConfirm;
  final bool selected;
  const GalleryRoomCard(
      {super.key, required this.item, this.onConfirm, this.selected = false});

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: MediaQuery.of(context).size.width - 80,
                  height: MediaQuery.of(context).size.width - 80,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(30),
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: CachedNetworkImageEnhanced(
                      imageUrl: item.avatarUrl,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 10),
                  child: Text(
                    item.name,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
                if (item.remark != '')
                  Container(
                    margin: const EdgeInsets.symmetric(
                      vertical: 10,
                      horizontal: 20,
                    ),
                    padding: const EdgeInsets.all(10),
                    width: double.infinity,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '简介：',
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        const SizedBox(height: 5),
                        SelectableText(
                          item.remark,
                          style: const TextStyle(
                            fontSize: 14,
                          ),
                        ),
                      ],
                    ),
                  ),
              ],
            ),
          ),
        ),
        if (onConfirm != null)
          Container(
            height: 70,
            margin: const EdgeInsets.only(top: 20),
            padding: const EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 10,
            ),
            child: Row(
              children: [
                WeakTextButton(
                  title: '取消',
                  onPressed: () {
                    context.pop();
                  },
                ),
                const SizedBox(width: 20),
                Expanded(
                  child: EnhancedButton(
                      title: selected ? '移除' : '选择',
                      onPressed: () {
                        onConfirm!();
                        context.pop();
                      }),
                )
              ],
            ),
          )
      ],
    );
  }
}
